package com.app.micaihu.view.newsdetail.adapter;

import android.view.View;
import android.view.ViewGroup;

import androidx.viewpager.widget.PagerAdapter;

import com.app.micaihu.bean.news.GalleryEntity;
import com.app.micaihu.bean.news.ImageDetailBean;
import com.app.micaihu.bean.news.RecommendGalleryEntity;
import com.app.micaihu.custom.view.PhotoImageView;
import com.app.micaihu.view.newsdetail.view.ImageAboutView;

import java.util.List;

/**
 * Created by Guo on 2016/7/29.
 *
 *
 * 图库的adapter---viewpager的
 *
 */
public class ImageDetailAdapter extends PagerAdapter {

    private List<GalleryEntity> galleryList;
    private List<RecommendGalleryEntity> recommendGalleryList;

    private String nextArticleId;

    private ImageAboutView view;
    private ImageDetailBean imageDetailBean;
    private PhotoImageView.ImagePhotoViewListener imagePhotoViewListener;




    public ImageDetailAdapter(ImageDetailBean imageDetailBean){

        this.imageDetailBean = imageDetailBean;
        if(imageDetailBean != null){
            this.galleryList = imageDetailBean.getGalleryList();
            this.recommendGalleryList = imageDetailBean.getRecommendGallery();
            this.nextArticleId = imageDetailBean.getNextArticleId();
        }
    }



    @Override
    public int getCount() {
        if(galleryList == null || galleryList.size()==0){//图片集合为空,返回0
            return 0;
        }else if(recommendGalleryList==null || recommendGalleryList.size()==0 ){//推荐集合为空,返回图片集合的size
            return galleryList.size();
        }else{//都不为空,返回图片+1的sizie
//            int i = galleryList.size() + 1;
            //LogUtils.e("gzq","viewpager的页码"+i+"");
            return galleryList.size() + 1;
        }

    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        //首先,推荐的list不能为空,并且已经划到最后一页,否则,就不创建gridview的视图
        if(recommendGalleryList != null && position == galleryList.size()){
            view = new ImageAboutView(container.getContext(),recommendGalleryList,nextArticleId);
            container.addView(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            return view;
        }else{
            PhotoImageView photoImageView = new PhotoImageView(container.getContext());
            photoImageView.loadImage(galleryList.get(position).getGalleryUrl(),container.getContext());

            if(imagePhotoViewListener != null){
                photoImageView.setImagePhotoViewListener(imagePhotoViewListener);
            }

            try {
                container.addView(photoImageView, ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return photoImageView;



        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //super.destroyItem(container, position, object);
        container.removeView((View) object);
    }

    public  void setImageClickListener(PhotoImageView.ImagePhotoViewListener imagePhotoViewListener){

        this.imagePhotoViewListener = imagePhotoViewListener;
    }

    /**
     * 返回ImageAboutView的方法
     * @return
     */
    public  ImageAboutView getImageAboutView(){
      return view;
    }


}
